package com.atguigu.eduservice.service.impl;

import com.alibaba.excel.EasyExcel;
import com.atguigu.eduservice.entity.EduSubject;
import com.atguigu.eduservice.entity.excel.SubjectData;
import com.atguigu.eduservice.entity.subject.OneSubject;
import com.atguigu.eduservice.entity.subject.TwoSubject;
import com.atguigu.eduservice.listener.SubjectExcelListener;
import com.atguigu.eduservice.mapper.EduSubjectMapper;
import com.atguigu.eduservice.service.EduSubjectService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2022-10-09
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {

    //添加课程分类
    @Override
    public void saveSubject(MultipartFile file,EduSubjectService subjectService) {
        try{
            //文件输入流
            InputStream in = file.getInputStream();
            //调用方法进行读取
            EasyExcel.read(in,SubjectData.class,new SubjectExcelListener(subjectService)).sheet().doRead();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    //课程分类列表
    @Override
    public List<OneSubject> getAllOneTwoSubject() {
        //查询一级分类 parent_id= 0
        QueryWrapper<EduSubject> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id","0");
        List<EduSubject> oneSubjectList = baseMapper.selectList(wrapper);

        //查询二级分类 parent_id ！= 0
        QueryWrapper<EduSubject> wrapper1 = new QueryWrapper<>();
        wrapper1.ne("parent_id","0");
        List<EduSubject> twoSubjectList = baseMapper.selectList(wrapper1);

        //创建list集合，用于存储最终封装数据
        List<OneSubject> finalSubjectList = new ArrayList<>();

        //封装一级分类
        //查询出来所有的一级分类list集合遍历，得到每一个一级分类对象，获取每一个一级分类对象的值
        //封装到要求的list集合里面
        for (int i = 0; i < oneSubjectList.size(); i++) {
            //得到oneSubjectList每个EduSubject对象
            EduSubject eduSubject = oneSubjectList.get(i);
            //把获取出来的值放到OneSubject对象里
            //多个OneSubject放到最终的集合里面
            OneSubject oneSubject = new OneSubject();
//            oneSubject.setId(eduSubject.getId());
//            oneSubject.setTitle(eduSubject.getTitle());
            BeanUtils.copyProperties(eduSubject,oneSubject);
            finalSubjectList.add(oneSubject);
            
            //在一级分类循环遍历查询所有的二级分类
            //创建list集合封装每一个一级分类的二级分类
            List<TwoSubject> twoSubject = new ArrayList<>();
            //遍历所有的二级分类
            for (int i1 = 0; i1 < twoSubjectList.size(); i1++) {
                //获取每一个二级分类
                EduSubject tSubject = twoSubjectList.get(i1);
                //判断二级分类parend_id是否和一级分类的id一致
                if (tSubject.getParentId().equals(eduSubject.getId())){
                    //把tSubject值封装到TwoSubjcet1中
                    TwoSubject twoSubject1 = new TwoSubject();
                    BeanUtils.copyProperties(tSubject,twoSubject1);
                    twoSubject.add(twoSubject1);
                }
            }
            oneSubject.setChildren(twoSubject);
        }

        return finalSubjectList;
    }
}
